package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;
import clientData.*;
import exception.*;
import java.util.LinkedList;
import java.util.Vector;
import java.rmi.Naming;
import server.*;
import java.sql.*;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class showPost_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(1);
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=utf-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n");
      out.write("<title>SHOW POST</title>\r\n");
      out.write("<script src=\"SpryAssets/SpryMenuBar.js\" type=\"text/javascript\"></script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!-- this script is notifing the user about his friends -->\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\" src=\"jquery.js\"></script>\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("setInterval(function() {\r\n");
      out.write("\t$.ajax({\r\n");
      out.write("\t\ttype : \"GET\",\r\n");
      out.write("\t\turl : \"notifier.jsp\",\r\n");
      out.write("\t\tsuccess : parseXml\r\n");
      out.write("\t});\r\n");
      out.write("}, 20000);\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("function parseXml(xml)\r\n");
      out.write("{\r\n");
      out.write("\t var msg = \"\";\r\n");
      out.write("  //find every Tutorial and print the author\r\n");
      out.write("  $(xml).find(\"MSG\").each(function()\r\n");
      out.write("  {\r\n");
      out.write("\t msg+= $(this).find(\"REASON\").text()+\".\\n\";\r\n");
      out.write("\t msg+= \"Added by user : \"+$(this).find(\"USER\").text()+\".\\n\";\r\n");
      out.write("\t msg+= \"To forum : \"+$(this).find(\"FORUM\").text()+\".\\n\";\r\n");
      out.write("\t msg+= \"In topic : \"+$(this).find(\"TOPIC\").text()+\".\\n\";\r\n");
      out.write("\t msg+= \"Post title = \"+$(this).find(\"POST\").text()+\".\\n\\n\";\r\n");
      out.write("\t  \r\n");
      out.write("  });\r\n");
      out.write(" \r\n");
      out.write("  if (msg!=\"\")\r\n");
      out.write("\t  alert(msg);\r\n");
      out.write("\r\n");
      out.write("  // Output:\r\n");
      out.write("  // The Reddest\r\n");
      out.write("  // The Hairiest\r\n");
      out.write("  // The Tallest\r\n");
      out.write("  // The Fattest\r\n");
      out.write("}\r\n");
      out.write("\r\n");
      out.write("</script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<link href=\"SpryAssets/SpryMenuBarVertical.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("<link href=\"screenStyle.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("</head>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write('\r');
      out.write('\n');



	int postId = Integer.parseInt((String)request.getParameter("postId"));
	session.setAttribute("postId",postId);
//	session.setAttribute("forumId",3);
	//session.setAttribute("topicId",4);

	

	if (session.getAttribute("userName")==null || session.getAttribute("forumId")==null
		|| session.getAttribute("topicId")==null)
	{
		response.sendRedirect("loggin.jsp");
		response.flushBuffer();
		return;
	}




      out.write("\r\n");
      out.write("\r\n");

//LinkedList<PostDataInt> posts = null;
//PostDataInt mypost = new PostData(postId,"CIVIC","The content of the post",new Date(),new Date(), new MemberData(3,"ari","Y@S.com","Cohen","Yosef"));
LinkedList<PostDataInt> posts = new LinkedList<PostDataInt>();
PostDataInt mypost = null;
try
{
	posts = forum.getPostList((String)session.getAttribute("userName"),(String)session.getAttribute("password"),(Integer)session.getAttribute("topicId"),(Integer)session.getAttribute("forumId"));
	for (PostDataInt _post:posts)
		if(_post.getId() == postId){
			mypost = _post;
			break;
		}
	if(mypost==null)
		throw new Exception("No such post with ID: "+postId);
}
catch(NotLoggedInException t)
{

	response.sendRedirect("loggin.jsp");
	response.flushBuffer();
	return;
	
}
catch(Exception e){		
	response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server."+e+" "+
			(String)session.getAttribute("userName")+" "+
			(String)session.getAttribute("password")+" "+
			postId+" "+
			session.getAttribute("forumId")+" "+
			session.getAttribute("postId"));
	response.flushBuffer();
	return;
}


      out.write("\r\n");
      out.write("\r\n");
	String user =  (String)session.getAttribute("userName");
	session.setAttribute("postTitle",mypost.getTitle());
	session.setAttribute("postContent",mypost.getContent());
	String topicName = request.getParameter("topicName");

      out.write("\r\n");
      out.write("<body>\r\n");
      out.write("\t<h1> Hello ");
      out.print( user);
      out.write(" !</h1>\r\n");
      out.write("\r\n");
      out.write("<ul id=\"MenuBar1\" class=\"MenuBarVertical\">\r\n");
      out.write("      <li><a class=\"MenuBarItemSubmenu\" href=\"friends.jsp\">manage friends</a></li>\r\n");
      out.write("  \t  <li><a href=\"index.jsp\" onClick= \"logoutFunc()\">logout</a>  </li>\r\n");
      out.write("  \t  ");

		if (mypost.getCreator().getUserName().equals(user)){
			
      out.write("\r\n");
      out.write("  \t  <li><a href=\"editPost.jsp?topicName=");
      out.print(topicName);
      out.write("\">edit Post</a>  </li>\r\n");
      out.write("  \t ");
}
      out.write("\r\n");
      out.write("     </ul>\r\n");
      out.write("   \r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("    var MenuBar1 = new Spry.Widget.MenuBar(\"MenuBar1\", {imgRight:\"SpryAssets/SpryMenuBarRightHover.gif\"});\r\n");
      out.write("    </script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("function logoutFunc()\r\n");
      out.write("{\r\n");
      out.write("\t\r\n");
      out.write("var xmlhttp;\r\n");
      out.write("\r\n");
      out.write("if (window.XMLHttpRequest)\r\n");
      out.write("  {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("  xmlhttp=new XMLHttpRequest();\r\n");
      out.write("  }\r\n");
      out.write("else\r\n");
      out.write("  {// code for IE6, IE5\r\n");
      out.write("  xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("  }\r\n");
      out.write("/*xmlhttp.onreadystatechange=function()\r\n");
      out.write("  {\r\n");
      out.write("  if (xmlhttp.readyState==4 && xmlhttp.status==200)\r\n");
      out.write("    {\r\n");
      out.write("\t    \r\n");
      out.write("    document.getElementById(\"myDiv\").innerHTML=xmlhttp.responseText;\r\n");
      out.write("\r\n");
      out.write("    }\r\n");
      out.write("  }\r\n");
      out.write("*/\r\n");
      out.write("  \r\n");
      out.write("xmlhttp.open(\"GET\",\"logout.jsp\",false);\r\n");
      out.write("//http.open(\"POST\", \"userValidation.jsp\", true);\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("xmlhttp.send();\r\n");
      out.write("return xmlhttp.responseText;\r\n");
      out.write("}\r\n");
      out.write(" </script>\r\n");
      out.write("\r\n");
      out.write("<br>\r\n");
      out.write("<br>    \r\n");
      out.write("<table border=2 align=\"center\">\r\n");
      out.write("<tr><th><font size=11> ");
      out.print(mypost.getTitle());
      out.write("</font></th></tr>\r\n");
      out.write("<tr><td  width=\"600\" height=\"200\"><font size=6>");
      out.print(mypost.getContent());
      out.write("</font><br><br>\r\n");
      out.write("<font color=\"gray\" size=5> ");
      out.print(mypost.getCreator() );
      out.write(" , </font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"gray\" size=3>  ");
      out.print(mypost.getCreationTime() );
      out.write(" <br>\r\n");
      out.write("\t\tLast modified: ");
      out.print(mypost.getEditingTime() );
      out.write("</font>\r\n");
      out.write("</td></tr>\r\n");
      out.write("</table>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<form name=\"foo\" method=post action=\"editPost.jsp\">\r\n");
      out.write("\t<input type=\"hidden\" name=\"postId\" value=\"\">\r\n");
      out.write("\r\n");
      out.write("</form>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
